package cn.gemframe.response;

import java.io.Serializable;

import cn.gemframe.constant.GemConstant;
import cn.gemframe.exception.status.GemErrorStatus;
import com.alibaba.fastjson.JSONObject;
import lombok.*;

@Data
@ToString
@Builder
@NoArgsConstructor
@AllArgsConstructor
//@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResultData<T> implements Serializable {
	private static final long serialVersionUID = 6688136731432037929L;

	public static final Integer UNKONW = 0;
	public static final Integer SUCCESS = 1;
	public static final Integer FAIL = 2;

	private Long code;
	private String msg;
	private Object data;


	/**
	 * 构造一个正常返回结果
	 * @return
	 */
	public static <X> ResultData<X> SUCCESS(X data){
		GemErrorStatus status = GemErrorStatus.OK;
		return ResultData.<X>builder()
				.code(status.getCode())
				.msg(status.getMsg())
				.data(data)
				.build();
	}

	/**
	 * 构造一个服务器异常的返回结果
	 * @return
	 */
	public static <T> ResultData<T>  getServerErrorResult(Long code,String msg){
		GemErrorStatus status = GemErrorStatus.SYSTEM_ERROR;
		return ResultData.<T>builder()
				.code(code)
				.msg(msg)
				.data(null)
				.build();
	}

	/**
	 * 构造一个服务器异常的返回结果
	 * @return
	 */
	public static <T> ResultData<T>  getServerErrorResult(String msg){
		GemErrorStatus status = GemErrorStatus.SYSTEM_ERROR;
		return ResultData.<T>builder()
				.code(status.getCode())
				.msg(msg)
				.data(null)
				.build();
	}

	/**
	 * 构造一个服务器异常的返回结果
	 * @return
	 */
	public static <T> ResultData<T>  getServerErrorResult(){
		GemErrorStatus status = GemErrorStatus.SYSTEM_ERROR;
		return ResultData.<T>builder()
				.code(status.getCode())
				.msg(status.getMsg())
				.data(null)
				.build();
	}

	/**
	 * 构造一个参数异常的返回结果
	 * @return
	 */
	public static <T> ResultData<T>  getIllegalArgumentResult(){
		GemErrorStatus status = GemErrorStatus.PARAM_ERROR;
		return ResultData.<T> builder()
				.code(status.getCode())
				.msg(status.getMsg())
				.data(null)
				.build();
	}

	/**
	 * 构造一个参数异常的返回结果
	 * @return
	 */
	public static <T> ResultData<T>  getIllegalArgumentResult(String msg){
		GemErrorStatus status = GemErrorStatus.PARAM_ERROR;
		return ResultData.<T> builder()
				.code(status.getCode())
				.msg(msg)
				.data(null)
				.build();
	}


	/**
	 * 构造一个数据库操作异常的返回结果
	 * @return
	 */
	public static <T> ResultData<T>  getDBOperError(Integer operType){
		GemErrorStatus status;
		if(operType == GemConstant.DBOperType.INSERT){
			status = GemErrorStatus.DB_INSERT_ERROR;
		}else if(operType == GemConstant.DBOperType.DELETE){
			status = GemErrorStatus.DB_DELETE_ERROR;
		}else if(operType == GemConstant.DBOperType.UPDATE){
			status = GemErrorStatus.DB_UPDATE_ERROR;
		}else if(operType == GemConstant.DBOperType.SELECT){
			status = GemErrorStatus.DB_SELECT_ERROR;
		}else{
			status = GemErrorStatus.DB_SELECT_ERROR;
		}
		return ResultData.<T> builder()
				.code(status.getCode())
				.msg(status.getMsg())
				.data(null)
				.build();
	}

	/**
	 * 获取指定code的结果
	 * @param status
	 * @return
	 */
	public static <T> ResultData<T>  getResultWithCode(GemErrorStatus status){
		return ResultData.<T> builder()
				.code(status.getCode())
				.msg(status.getMsg())
				.data(null)
				.build();
	}

	/**
	 * 获取指定code的结果
	 * @param status
	 * @param data
	 * @return
	 */
	public static <T> ResultData<T>  getResultWithCode(GemErrorStatus status, T data){
		return ResultData.<T> builder()
				.code(status.getCode())
				.msg(status.getMsg())
				.data(data)
				.build();
	}


	public static boolean checkDBOperResult(Integer result) {
		if(null == result || result != GemConstant.Number.ONE){
			return false;
		}
		return true;
	}
}
